/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OperatorsTypeEditor.java
 *
 * Created on 2011-11-26, 17:50:44
 */
package pobicos;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import operators.OperatorImpl;
/**
 *
 * @author marcin
 */
public class OperatorsTypeEditor extends javax.swing.JFrame {
    
    private SubtreeEditor subTreeEditor_;
    private OperatorImpl fileImplPath_=null;
    
    /** Creates new form OperatorsTypeEditor */
    public OperatorsTypeEditor(final SubtreeEditor subTreeEditor) {
        initComponents();
        subTreeEditor_= subTreeEditor;
        this.setLocationRelativeTo(subTreeEditor_);
        CreateNewTypeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Operator o = new Operator(nameTextField.getText(), 
                        Integer.parseInt(inputTextField.getText()), Integer.parseInt(outputTextField.getText()));
                //Dodanie implementacji operatora
                        o.setFileImplName(fileImplPath_);
                        if( subTreeEditor_.getOperatorsList()!=null) {
                            subTreeEditor_.getOperatorsList().add(o); 
                        } else {
                            subTreeEditor_.setNewOperatorsList();
                            subTreeEditor_.getOperatorsList().add(o); 
                        }
                
                SubtreeEditor.OperatorsComboBox.addItem(nameTextField.getText()+":"+inputTextField.getText()+":"+outputTextField.getText());
                SubtreeEditor.OperatorsComboBox.setSelectedItem(nameTextField.getText()+":"+inputTextField.getText()+":"+outputTextField.getText());
                dispose();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        newOperatorLabel = new javax.swing.JLabel();
        inputLabel = new javax.swing.JLabel();
        outputLabel = new javax.swing.JLabel();
        inputTextField = new javax.swing.JTextField();
        outputTextField = new javax.swing.JTextField();
        CreateNewTypeButton = new javax.swing.JButton();
        CloseButton = new javax.swing.JButton();
        loadImpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pobicos.AdaptSysApp.class).getContext().getResourceMap(OperatorsTypeEditor.class);
        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N
        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });

        nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        newOperatorLabel.setFont(resourceMap.getFont("newOperatorLabel.font")); // NOI18N
        newOperatorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        newOperatorLabel.setText(resourceMap.getString("newOperatorLabel.text")); // NOI18N
        newOperatorLabel.setName("newOperatorLabel"); // NOI18N

        inputLabel.setText(resourceMap.getString("inputLabel.text")); // NOI18N
        inputLabel.setName("inputLabel"); // NOI18N

        outputLabel.setText(resourceMap.getString("outputLabel.text")); // NOI18N
        outputLabel.setName("outputLabel"); // NOI18N

        inputTextField.setText(resourceMap.getString("inputTextField.text")); // NOI18N
        inputTextField.setName("inputTextField"); // NOI18N

        outputTextField.setText(resourceMap.getString("outputTextField.text")); // NOI18N
        outputTextField.setName("outputTextField"); // NOI18N
        outputTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputTextFieldActionPerformed(evt);
            }
        });

        CreateNewTypeButton.setText(resourceMap.getString("CreateNewTypeButton.text")); // NOI18N
        CreateNewTypeButton.setName("CreateNewTypeButton"); // NOI18N

        CloseButton.setText(resourceMap.getString("CloseButton.text")); // NOI18N
        CloseButton.setName("CloseButton"); // NOI18N
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        loadImpButton.setText(resourceMap.getString("loadImpButton.text")); // NOI18N
        loadImpButton.setName("loadImpButton"); // NOI18N
        loadImpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadImpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(inputLabel)
                                .addComponent(outputLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(inputTextField, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(outputTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
                                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(24, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(loadImpButton, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(CreateNewTypeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                            .addComponent(CloseButton)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(newOperatorLabel)
                        .addGap(82, 82, 82))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(newOperatorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputLabel)
                    .addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputLabel)
                    .addComponent(outputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(loadImpButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CreateNewTypeButton)
                    .addComponent(CloseButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameTextFieldActionPerformed

    private void outputTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_outputTextFieldActionPerformed

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void loadImpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadImpButtonActionPerformed

        JFileChooser chooser = new JFileChooser();
        try {
            // Create a File object containing the canonical path of the desired directory
            File f = new File(new File(".").getCanonicalPath() + "/operators_impl");
            // Set the current directory
            chooser.setCurrentDirectory(f);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Java .class", "class");
            chooser.setFileFilter(filter);

        } catch (IOException e) {
        }
        int returnVal = chooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();

            try {
                URI uri = file.toURI();
                URL url = uri.toURL();
                URL[] urls = new URL[]{url};

                ClassLoader cl = new URLClassLoader(urls);

                StringTokenizer st = new StringTokenizer(file.getName(), ".");
                String s = st.nextToken();
                
                Class c = cl.loadClass("operators." + s);
                OperatorImpl o = (OperatorImpl) c.newInstance();
                
                fileImplPath_ = o;

                System.out.println("IMPL : " + o.toString());
                

            } catch (ClassNotFoundException w) {
                System.out.print("Class not found : " + w.getStackTrace());
            } catch (InstantiationException w) {
            } catch (IllegalAccessException w) {
            } catch (MalformedURLException w) {
            }
                                          
        }

    }//GEN-LAST:event_loadImpButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CloseButton;
    private javax.swing.JButton CreateNewTypeButton;
    private javax.swing.JLabel inputLabel;
    private javax.swing.JTextField inputTextField;
    private javax.swing.JButton loadImpButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel newOperatorLabel;
    private javax.swing.JLabel outputLabel;
    private javax.swing.JTextField outputTextField;
    // End of variables declaration//GEN-END:variables
}
